<template>
    <div>
        <div class="container">
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
                <el-tab-pane label="文本列表" name="1">
                    <div class="handle-box" border></div>

                    <el-tabs type="card">
                        <el-table border :data="lists" v-loading="loading"  element-loading-text="拼命加载中"   element-loading-background="rgb(230 230 230 / 80%)"  element-loading-spinner="el-icon-loading">
                            <el-table-column label="序号" width="50">
                                <template slot-scope="scope">
                                    <span>{{(scope.$index+1)}}</span>
                                </template>
                                
                            </el-table-column>
                            
                              <el-table-column prop="id" label="ID" width="50"></el-table-column>
                              <el-table-column prop="title" label="标题"></el-table-column>
                           
                             <el-table-column label="二维码(查看大图)" align="center">
                                <template slot-scope="scope">
                                    <el-image
                                        class="table-td-thumb"
                                        :src="scope.row.qr_url"
                                        :preview-src-list="[scope.row.qr_url]"
                                    ></el-image>
                                </template>
                            </el-table-column>
                            <el-table-column prop="created_at" label="创建时间"></el-table-column>
                            <el-table-column prop="updated_at" label="更新时间"></el-table-column>

                            <el-table-column label="操作" width="120" align="center" fixed="right">
                                <template slot-scope="scope">
                                      <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                                      <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                                  
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-tabs>

                </el-tab-pane>
                 <el-tab-pane label="创建文本" name="2">
                     <div class="title">
                            <div>
                                <div><span class="mark">*</span>标题：</div>
                                <div><el-input v-model="title"  placeholder="请输入标题"></el-input></div>
                            </div>
                      
                     </div>
                      <div class="text " >
                          <div id="demo1"></div>
                          
                             <el-button   v-loading.fullscreen.lock="fullscreenLoading" class="editor-btn" type="primary" @click="sub">提交</el-button>
                    </div>
                 </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import {baseURL} from '@/utils/request';
import fetchData from '../../assets/js/table';
//  import { quillEditor } from 'vue-quill-editor';
import wangEditor from 'wangeditor'

export default {
    name: 'basetable',
  
      components: {
            // quillEditor
        },
    data() {
        return {
            editor:null,
           
            fullscreenLoading: false,
            title:'',
               content: '',
                editorOption: {
                    placeholder: '请输入内容'
                },
            activeName: '1',
            editTypeid:'',
            lists: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
              loading: true,
            params: {
               
                page: 1,
                limit: 10,
               
            },
            addParams:{
                account:'',
                password:'',
            }
        };
    },
    mounted(){
        console.log("baseURL",baseURL)
        const editor = new wangEditor(`#demo1`)

            // 配置 onchange 回调函数，将数据同步到 vue 中
            editor.config.onchange = (newHtml) => {
            this.content = newHtml
            }
            editor.config.uploadImgServer = baseURL+'index/uploadFile'
             editor.config.uploadFileName = 'file_name'
             editor.config.uploadImgMaxLength = 1;
            
             editor.config.uploadImgHooks = {
                        // 上传图片之前
                       
                        // 图片上传并返回了结果，图片插入已成功
                        success: function(xhr) {
                            console.log('success', xhr)
                        },
                        // 图片上传并返回了结果，但图片插入时出错了
                        fail: function(xhr, editor, resData) {
                            console.log('fail', resData)
                        },
                        // 上传图片出错，一般为 http 请求的错误
                        error: function(xhr, editor, resData) {
                            console.log('error', xhr, resData)
                        },
                        // 上传图片超时
                        timeout: function(xhr) {
                            console.log('timeout')
                        },
                        // 图片上传并返回了结果，想要自己把图片插入到编辑器中
                        // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式，可使用 customInsert
                        customInsert: function(insertImgFn, result) {
                            // result 即服务端返回的接口
                            console.log('customInsert', result.data.url)

                            // insertImgFn 可把图片插入到编辑器，传入图片 src ，执行函数即可
                            insertImgFn( result.data.url)
                        }
                    }
            // 创建编辑器
            editor.create()

            this.editor = editor
    },
    created(option) {
        this.params.parent_id = this.$route.query.id;
       
                this.getList();
    },
    watch: {
        $route(to, from) {
            if (this.params.parent_id != this.$route.query.id) {
                this.params.parent_id = this.$route.query.id;
                this.getList();
            }
        }
    },
    methods: {
        sub(){
              let t=this;
          

            if(!t.title){
                return    t.$message.error("请填写标题");
            }
            if(!t.content){
                return    t.$message.error("请填写内容");
            }
           
         let d={
             content:t.content,
             title:t.title,
             user_id: JSON.parse(localStorage.getItem("userinfo")).id,
         }
        if(t.editTypeid){
            d.id=t.editTypeid;
        }
         this.fullscreenLoading = true;
               t._API.saveContent(d).then(res=>{
                    this.fullscreenLoading = false;
                        if(res.code===200){
                               t.$message.success('保存成功');
                                t.getList()
                               
                                t.activeName="1";
                        } else {
                                t.$message.error('保存失败');
                            
                                return false;
                            }
                    })
        
           
        },
        getList() {
            let t = this;
            let d = t.params;

            d.user_id= JSON.parse(localStorage.getItem("userinfo")).id,

            t._API.getList(d).then((res) => {
                t.loading=false;
                if (res.code == 200) {
                    
                    t.lists = res.data.lists;
                }
            });
        },
        handleClick(tab, event) {
             this.editor.txt.html('') // 重新设置编辑器内容
            this.editTypeid='';
            this.title='';
        },
        // // 获取 easy-mock 的模拟数据

        // // 触发搜索按钮
        handleSearch() {
            this.$set(this.params, 'page', 1);
            this.getList();
        },
        // // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
               let t=this;
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let d={id:row.id}
                          t._API.deleteContent(d).then(res=>{
            
                        if(res.code===200){
                                t.$message.success(res.message);
                                t.getList()
                                
                            
                        } else {
                                t.$message.error(res.message);
                            
                                return false;
                            }
                     })
                   
                   
                })
                .catch(() => {});
        },
        // // 多选操作
        handleSelectionChange(val) {
            //  this.multipleSelection = val;
        },
     
        // // 编辑操作
        handleEdit(index, row) {
           
            this.activeName='2';
             this.editTypeid=row.id;
              this.editor.txt.html(row.content) // 重新设置编辑器内容
           
 
              this.title=row.title;
        },
        // // 保存编辑
        // saveEdit() {
        //     this.editVisible = false;
        //     this.$message.success(`修改第 ${this.idx + 1} 行成功`);
        //     this.$set(this.tableData, this.idx, this.form);
        // },
        // // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped lang="less">
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

/deep/ .cell {
    font-size: 12px;
}
.text{
    padding:0 20px 0 10px;
   
}

.title{
    padding:0 20px 0 10px;
    >div{
        display: flex;
        align-items: center;
        margin-bottom:20px;
        >div:nth-child(1){
          
            font-size: 14px;
            font-weight: bold;
            width: 50px;
            

        }
        
            /deep/ .el-input__inner{
                border-radius: 0;
                width: 200px;
            }


            .but{
                margin-top: 10px;
            }
    }
}
.mark{
    color:red;
    font-size:14px;
    
    left:93px;
    top:8px;
}
  .editor-btn{
        margin-top: 20px;
    }
</style>
